import type {
  ActionsProps,
  FetchConfig,
  ObjectProps,
  vxeColumnConfigType
} from "../type";
import { ACTION_TYPE } from "../type";
// import { computed } from "vue";
// import { useConfigHook } from "@/store/modules/configApi";
import type { VxeColumnProps, VxeGridProps } from "vxe-table";
import { isFunction } from "@pureadmin/utils";
import { cloneDeep } from "lodash-unified";

// const langList = computed(() => useConfigHook().getLabelList);
export const propsDefinition = {
  // 表格标题
  tableTitle: {
    type: String,
    default: ""
  },
  showIndex: {
    type: Boolean,
    default: true
  },
  showCheckbox: {
    type: Boolean,
    default: false
  },
  isServerPage: {
    type: Boolean,
    default: false
  },
  showExportBtn: {
    type: Boolean,
    default: true
  },
  height: {
    type: [String, Number]
  },
  columns: {
    type: Array<ColumnMapType>,
    default: () => []
  },
  vxeColumnConfig: {
    type: Object as PropType<vxeColumnConfigType>,
    default: () => ({})
  },
  dataSource: {
    type: Array<{ [key: string]: any }>,
    default: () => []
  },
  fetchConfig: {
    type: Object as PropType<FetchConfig>,
    default: () => ({})
  },
  // 固定操作栏 key 集合
  actions: {
    type: Array<ACTION_TYPE>,
    default: () => []
  },
  // 头部额外操作集合
  topTools: {
    type: Array<ActionsProps>,
    default: () => []
  },
  // 操作栏额外操作集合
  operateTools: {
    type: Array<ActionsProps>,
    default: () => []
  },
  vxeTableConfig: {
    type: Object as PropType<VxeGridProps<ObjectProps>>,
    default: () => ({})
  },
  operateDisableHandle: {
    type: Object as PropType<{
      [key: ACTION_TYPE | string]: (row: ObjectProps) => boolean;
    }>,
    default: () => ({})
  }
};

export const topActionList: ActionsProps[] = [
  {
    icon: "ph:plus",
    key: ACTION_TYPE.TADD,
    seq: 1,
    name: "table.add"
  },
  {
    icon: "table-refresh",
    key: ACTION_TYPE.REFRESH,
    seq: 10,
    plain: true,
    name: "table.refresh"
  },
  {
    icon: "table-Import",
    key: ACTION_TYPE.UPLOAD,
    seq: 20,
    name: "table.upload"
  },
  {
    icon: "table-print",
    key: ACTION_TYPE.PRINT,
    seq: 30,
    name: "table.print"
  },
  {
    icon: "table-fullscreen",
    key: ACTION_TYPE.FULLSCREEN,
    name: "table.fullscreen",
    seq: 40,
    onClick: (row, vxeGridRef) => {
      const $grid = vxeGridRef.value;
      if ($grid) {
        $grid.zoom();
      }
    }
  },
  {
    icon: "",
    name: "table.putaway",
    seq: 20,
    plain: true,
    key: ACTION_TYPE.TPUTAWAY
  },
  {
    icon: "",
    name: "table.soldOut",
    seq: 30,
    plain: true,
    key: ACTION_TYPE.TOFFSHELVE
  },
  {
    icon: "table-Export",
    key: ACTION_TYPE.DOWNLOAD,
    seq: 50,
    name: "table.export"
    // onClick: () => {
    //   exportExcel(columnData.value, props.dataSource, langList.value[props.tableName]);
    // }
  },
  {
    icon: "table-delete",
    status: "danger",
    plain: true,
    key: ACTION_TYPE.TDELETE,
    seq: 120,
    disabled: true,
    name: "table.multipleDelete"
  }
];

export const operateList: ActionsProps[] = [
  {
    icon: "table-view",
    name: "table.detail",
    status: "primary",
    seq: 1,
    key: ACTION_TYPE.DETAIL
  },
  {
    icon: "table-view",
    name: "table.view",
    status: "primary",
    seq: 1,
    key: ACTION_TYPE.VIEW
  },
  {
    icon: "lucide:edit",
    name: "table.edit",
    seq: 10,
    key: ACTION_TYPE.EDIT
  },
  {
    icon: "",
    name: "table.putaway",
    seq: 20,
    key: ACTION_TYPE.PUTAWAY
  },
  {
    icon: "",
    name: "table.soldOut",
    seq: 30,
    key: ACTION_TYPE.OFFSHELVE
  },
  {
    icon: "uiw:delete",
    name: "buttons.delete",
    status: "primary",
    seq: 120,
    key: ACTION_TYPE.DELETE
  }
];

/**
 * Generate an array of column objects based on the input columns and properties.
 *
 * @param {ColumnMapType[]} columns - an array of column objects
 * @param {{ [key: string]: VxeColumnProps }} property - an object containing properties for columns
 * @return {Array} an array of column objects
 */
type ColumnMapType = Record<string, any>;
export const handleColumnMap = (
  columns: ColumnMapType[],
  property: { [key: string]: VxeColumnProps },
  { t, te }: { t: (key: string) => string; te: (key: string) => boolean }
) => {
  if (!columns || !columns.length) return [];

  const columnResult = [];

  for (const column of columns) {
    columnResult.push({
      // field: column.COL_NAME,
      // title: column.LABEL_ID,
      // width: Number(column.COL_WIDTH),
      // align: column.TEXT_H_ALIGN ? column.TEXT_H_ALIGN.toLowerCase() : "left",
      // sortable: true,
      // visible: column.COL_HIDDEN_YN === "N",
      align: "center",
      ...column,
      title: te(column.title) ? t(column.title) : column.title,
      exportMethod: isFunction(column.exportMethod)
        ? column.exportMethod
        : ({ row }) => {
            if (
              column.field === "publish_status" ||
              column.field === "publishStatus"
            ) {
              return {
                0: "下架",
                1: "上架"
              }[row[column.field]];
            }
            return row[column.field];
          },
      formatter: column.formatter
        ? column.formatter
        : ({ cellValue }) => {
            if (!cellValue && cellValue !== 0) {
              return "/";
            }
            return cellValue;
          },
      ...(property[column.field] ?? {})
    });
  }
  return columnResult;
};

/**
 * Generate a new data page based on the current page and page size.
 *
 * @param {ObjectProps[]} data - Array of objects to paginate
 * @param {number} currentPage - The current page number
 * @param {number} pageSize - The number of items per page
 * @return {ObjectProps[]} A new page of data based on the current page and page size
 */
export const dataPageHandle = (
  data: ObjectProps[],
  currentPage: number,
  pageSize: number
): ObjectProps[] => {
  const start = (currentPage - 1) * pageSize;
  const end = currentPage * pageSize;
  return cloneDeep(data).slice(start, end);
};

export const handleEmptyData = (data: ObjectProps[]) => {
  return data.map(v => {
    // for (const vKey in v) {
    //   if (v[vKey] === null || v[vKey] === undefined) {
    //     v[vKey] = "";
    //   }
    // }
    return v;
  });
};

export const actionPrevClick = async (type: ACTION_TYPE) => {
  let result = { type: "" };
  switch (type) {
    case ACTION_TYPE.DELETE:
      // result = await window.Alert("Question", "COMS-0005");
      result.type = "Ok";
      break;
    default:
      result.type = "Ok";
      break;
  }
  return result.type === "Ok";
};

export const handleVal = (val: Record<string, any>) => {
  const res = {};
  for (const key in val) {
    res[key] = val[key] === "/" ? "" : val[key];
  }
  return res;
};
